/*
* ServerSelectGUI.java
*
* Created on 17 mars 2011, 20:32:38
*/
package mncservermon;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import javax.swing.JOptionPane;
import org.jasypt.util.text.BasicTextEncryptor;
/**
*
* @author Llamatron
*/
public class ServerSelectGUI extends javax.swing.JFrame {
private ArrayList ServersList;
private int len;
private BasicTextEncryptor encryptor;
private int cursel;
/** Creates new form ServerSelectGUI */
public ServerSelectGUI(Properties ConfigData) {
initComponents();
encryptor = new BasicTextEncryptor();
encryptor.setPassword("XQ1kg3L3zjPEnZZTtu01dohfPEyiwW0TzrUVwiPcjuZKIgVxD8qRnvK64j5xJsq");
len = ConfigData.size() / 5;
ServersList = new ArrayList();
int c = 0;
do {
Map ServerMap = new HashMap();
ServerMap.put("Name", ConfigData.getProperty("SERVER_NAME_" + c));
ServerMap.put("Address", ConfigData.getProperty("SERVER_ADDRESS_" + c));
ServerMap.put("Port", ConfigData.getProperty("SERVER_PORT_" + c));
ServerMap.put("Login", ConfigData.getProperty("SERVER_LOGIN_" + c));
ServerMap.put("Pass", ConfigData.getProperty("SERVER_PASS_" + c));
ServersList.add(ServerMap);
this.ServerComboBox.addItem((c + 1) + ": " + ServerMap.get("Name"));
c++;
} while (c < len);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
ServerComboBox = new javax.swing.JComboBox();
jPanel1 = new javax.swing.JPanel();
NameField = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
AddressField = new javax.swing.JTextField();
LoginField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
PassField = new javax.swing.JPasswordField();
VerifyField = new javax.swing.JPasswordField();
jLabel6 = new javax.swing.JLabel();
PortField = new javax.swing.JFormattedTextField();
LaunchButton = new javax.swing.JButton();
LoadButton = new javax.swing.JButton();
SaveButton = new javax.swing.JButton();
DeleteButton = new javax.swing.JButton();
NewButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
ServerComboBox.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
ServerComboBox.setFocusable(false);
jPanel1.setBackground(new java.awt.Color(204, 204, 204));
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
NameField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
NameField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
NameFieldKeyReleased(evt);
}
});
jLabel2.setText("Port");
AddressField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
AddressFieldKeyReleased(evt);
}
});
jLabel1.setText("Address");
jLabel3.setText("Login");
jLabel5.setText("Name");
jLabel4.setText("Password");
PassField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
PassFieldKeyReleased(evt);
}
});
VerifyField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
VerifyFieldKeyReleased(evt);
}
});
jLabel6.setText("Verify");
PortField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#"))));
PortField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
PortFieldKeyReleased(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(NameField, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addComponent(AddressField, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addComponent(PortField, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(LoginField, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addComponent(PassField, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addComponent(VerifyField, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {AddressField, LoginField, NameField, PassField, PortField, VerifyField});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(NameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(AddressField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(PortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LoginField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(PassField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(VerifyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
LaunchButton.setText("Launch");
LaunchButton.setFocusPainted(false);
LaunchButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
LaunchButtonMouseClicked(evt);
}
});
LoadButton.setText("Load");
LoadButton.setFocusPainted(false);
LoadButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
LoadButtonMouseClicked(evt);
}
});
SaveButton.setText("Save");
SaveButton.setEnabled(false);
SaveButton.setFocusPainted(false);
SaveButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
SaveButtonMouseClicked(evt);
}
});
DeleteButton.setText("Delete");
DeleteButton.setFocusPainted(false);
DeleteButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
DeleteButtonMouseClicked(evt);
}
});
NewButton.setText("New");
NewButton.setFocusPainted(false);
NewButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
NewButtonMouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ServerComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(LaunchButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
.addComponent(LoadButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
.addComponent(SaveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
.addComponent(NewButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
.addComponent(DeleteButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ServerComboBox, jPanel1});
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {DeleteButton, LaunchButton, LoadButton, NewButton, SaveButton});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(LaunchButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(LoadButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SaveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(NewButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(DeleteButton))
.addGroup(layout.createSequentialGroup()
.addComponent(ServerComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void LoadButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoadButtonMouseClicked
int index = this.ServerComboBox.getSelectedIndex();
Map ServerMap = (Map) ServersList.get(index);
this.NameField.setText(ServerMap.get("Name").toString());
this.AddressField.setText(ServerMap.get("Address").toString());
this.PortField.setText(ServerMap.get("Port").toString());
this.LoginField.setText(ServerMap.get("Login").toString());
this.PassField.setText(encryptor.decrypt(ServerMap.get("Pass").toString()));
this.VerifyField.setText(encryptor.decrypt(ServerMap.get("Pass").toString()));
}//GEN-LAST:event_LoadButtonMouseClicked
private void SaveButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SaveButtonMouseClicked
if (this.NameField.getText().equals("") || this.AddressField.getText().equals("") || this.PortField.getText().equals("") || this.LoginField.getText().equals("") || this.PassField.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please fill all the fields", "Server description incomplete", JOptionPane.ERROR_MESSAGE);
return;
}
int index = this.ServerComboBox.getSelectedIndex();
String current = this.ServerComboBox.getItemAt(index).toString();
int cblen = this.ServerComboBox.getItemCount();
if (index < len) {
if (JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite " + current + "?", "Overwriting a server", 2, JOptionPane.QUESTION_MESSAGE) == 0) {
this.replaceServer(index,
this.NameField.getText(),
this.AddressField.getText(),
this.PortField.getText(),
this.LoginField.getText(),
this.PassField.getText());
}
} else {
int ind = 0;
do {
String nam = this.ServersList.get(ind).toString();
if (nam.equals(this.NameField.getText())) {
JOptionPane.showMessageDialog(null, "A server with that name already exists!", "Invalid name", JOptionPane.ERROR_MESSAGE);
return;
}
ind++;
} while (ind < len);
this.addServer(this.NameField.getText(),
this.AddressField.getText(),
this.PortField.getText(),
this.LoginField.getText(),
this.PassField.getText());
JOptionPane.showMessageDialog(null, "Server saved", "Completed", JOptionPane.INFORMATION_MESSAGE);
}
}//GEN-LAST:event_SaveButtonMouseClicked
private void NewButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_NewButtonMouseClicked
this.ServerComboBox.addItem("New Server");
int cblen = this.ServerComboBox.getItemCount() - 1;
this.ServerComboBox.setSelectedIndex(cblen);
this.NameField.setText(null);
this.AddressField.setText(null);
this.PortField.setText(null);
this.LoginField.setText(null);
this.PassField.setText(null);
}//GEN-LAST:event_NewButtonMouseClicked
private void DeleteButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DeleteButtonMouseClicked
int sel = this.ServerComboBox.getSelectedIndex();
if (JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + this.ServerComboBox.getItemAt(sel) + "?", "Deleting a server", 2, JOptionPane.QUESTION_MESSAGE) == 0) {
this.deleteServer(sel);
}
}//GEN-LAST:event_DeleteButtonMouseClicked
private void LaunchButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LaunchButtonMouseClicked
int index = this.ServerComboBox.getSelectedIndex();
Map ServerMap = (Map) ServersList.get(index);
String Name = ServerMap.get("Name").toString();
String Adress = ServerMap.get("Address").toString();
int Port = Integer.parseInt(ServerMap.get("Port").toString());
String Login = ServerMap.get("Login").toString();
String Pass = ServerMap.get("Pass").toString();
mncservermon.MNCServer serv = new mncservermon.MNCServer(Adress, Port, Login, Pass);
mncservermon.MonitorGUI mncgui = new mncservermon.MonitorGUI(serv);
mncgui.setTitle("MNC Server Monitor: " + Name);
mncgui.setLocationRelativeTo(null);
mncgui.setVisible(true);
this.setVisible(false);
}//GEN-LAST:event_LaunchButtonMouseClicked
private void VerifyFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_VerifyFieldKeyReleased
this.verifyData();
}//GEN-LAST:event_VerifyFieldKeyReleased
private void PassFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PassFieldKeyReleased
this.verifyData();
}//GEN-LAST:event_PassFieldKeyReleased
private void NameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NameFieldKeyReleased
this.verifyData();
}//GEN-LAST:event_NameFieldKeyReleased
private void AddressFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_AddressFieldKeyReleased
this.verifyData();
}//GEN-LAST:event_AddressFieldKeyReleased
private void PortFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PortFieldKeyReleased
verifyData();
}//GEN-LAST:event_PortFieldKeyReleased
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField AddressField;
private javax.swing.JButton DeleteButton;
private javax.swing.JButton LaunchButton;
private javax.swing.JButton LoadButton;
private javax.swing.JTextField LoginField;
private javax.swing.JTextField NameField;
private javax.swing.JButton NewButton;
private javax.swing.JPasswordField PassField;
private javax.swing.JFormattedTextField PortField;
private javax.swing.JButton SaveButton;
private javax.swing.JComboBox ServerComboBox;
private javax.swing.JPasswordField VerifyField;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables
private void addServer(String Name, String Address, String Port, String Login, String Pass) {
Map ServerMap = new HashMap();
ServerMap.put("Name", Name);
ServerMap.put("Address", Address);
ServerMap.put("Port", Port);
ServerMap.put("Login", Login);
ServerMap.put("Pass", encryptor.encrypt(Pass));
this.ServersList.add(ServerMap);
this.synchronizeConf(this.ServersList.size());
}
private void replaceServer(int index, String Name, String Address, String Port, String Login, String Pass) {
Map ServerMap = new HashMap();
ServerMap.put("Name", Name);
ServerMap.put("Address", Address);
ServerMap.put("Port", Port);
ServerMap.put("Login", Login);
ServerMap.put("Pass", encryptor.encrypt(Pass));
this.ServersList.set(index, ServerMap);
this.synchronizeConf(index);
}
private void deleteServer(int index) {
this.ServersList.remove(index);
this.synchronizeConf(index - 1);
}
private void synchronizeConf(int index) {
Properties ConfigData = new Properties();
do {
this.ServerComboBox.removeItemAt(0);
} while (this.ServerComboBox.getItemCount() != 0);
len = this.ServersList.size();
int c = 0;
do {
Map ServerMap = (HashMap) ServersList.get(c);
ConfigData.put("SERVER_NAME_" + c, ServerMap.get("Name").toString());
this.ServerComboBox.addItem((c + 1) + ": " + ServerMap.get("Name").toString());
ConfigData.put("SERVER_ADDRESS_" + c, ServerMap.get("Address").toString());
ConfigData.put("SERVER_PORT_" + c, ServerMap.get("Port").toString());
ConfigData.put("SERVER_LOGIN_" + c, ServerMap.get("Login").toString());
ConfigData.put("SERVER_PASS_" + c, ServerMap.get("Pass").toString());
c++;
} while (c < len);
File ConfigFile = new File("config.properties");
Writer writer = null;
try {
if (!ConfigFile.exists()) {
ConfigFile.createNewFile();
}
writer = new BufferedWriter(new FileWriter(ConfigFile));
ConfigData.store(writer, "MNC Server Monitor configuration file");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e);
}
this.ServerComboBox.setSelectedIndex(index);
}
}
private void verifyData() {
if (Arrays.equals(this.PassField.getPassword(), this.VerifyField.getPassword())
&& !this.NameField.getText().equals("")
&& !this.AddressField.getText().equals("")
&& !this.PortField.getText().equals("")) {
this.SaveButton.setEnabled(true);
} else {
this.SaveButton.setEnabled(false);
}
}
}